package cn.sxgan.gen.controller;

import cn.sxgan.gen.enums.DataSourceType;
import cn.sxgan.gen.model.DbTable;
import cn.sxgan.gen.model.GlobalDataSource;
import cn.sxgan.gen.services.base.DataSourceQueryFactory;
import cn.sxgan.gen.services.base.DataSourceService;
import cn.sxgan.gen.utils.CommonUtils;
import cn.sxgan.gen.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/datasource")
public class DataSourceController {
    
    @Autowired
    private DataSourceService dataSourceService;
    
    @Autowired
    private DataSourceQueryFactory dataSourceQueryFactory;
    
    @GetMapping("/findDataSourceList")
    public Result<List<GlobalDataSource>> getDataSources(@RequestParam Long pageNum,
                                                         @RequestParam Long pageSize) {
        // 获取数据源
        List<GlobalDataSource> dataSources = dataSourceService.getAllDataSources();
        List<GlobalDataSource> limit = dataSources.stream().skip((pageNum - 1) * pageSize).limit(pageSize).toList();
        return Result.success(limit, dataSources.size());
    }
    
    @PostMapping("/addDatasource")
    public Result<String> addDatasource(@RequestBody GlobalDataSource globalDataSource) {
        try {
            String dbUrl = CommonUtils.getDbUrl(globalDataSource);
            DataSourceType dataSourceType = DataSourceType.getEnumValue(globalDataSource.getDbType());
            if (dataSourceType == null) {
                return Result.fail("不支持的数据库类型");
            }
            dataSourceService.addDatasource(
                    globalDataSource.getKey(),
                    dbUrl,
                    globalDataSource.getUsername(),
                    globalDataSource.getPassword(),
                    dataSourceType.getDriverClass());
            return Result.success(null, "数据源添加成功");
        } catch (Exception e) {
            return Result.fail("添加失败: " + e.getMessage());
        }
    }
    
    
    @GetMapping("/findSelectDatasourceList")
    public Result<List<GlobalDataSource>> findSelectDatasourceList() {
        // 获取所有数据源选择列表
        List<GlobalDataSource> dataSources = dataSourceService.getAllDataSources();
        // 获取当前激活的数据源
        GlobalDataSource activeDataSource = dataSourceService.getActiveDataSource();
        for (GlobalDataSource dataSource : dataSources) {
            dataSource.setActive(dataSource.getKey().equals(activeDataSource.getKey()));
        }
        return Result.success(dataSources);
    }
    
    @GetMapping("/tables")
    @ResponseBody
    public Result<List<DbTable>> getTables(@RequestParam Long pageNum,
                                           @RequestParam Long pageSize) throws SQLException {
        return dataSourceService.getAllTables(pageNum, pageSize);
    }
}